/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Component;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.layouts.BorderLayout;
import java.util.Timer;
import java.util.TimerTask;
import utils.DateUtils;

/**
 *
 * @author Kvest
 */
public class BasicForm extends Form implements Runnable
{
    private static final int TITLE_ELEMENT_PADDING = 6;
    private static final int STATE_LABEL_WIDTH = 5;
    private static final int UPDATE_TIMER_INTERVAL = 5000;
    
    private static final int NO_STATE_COLOR = 0xdddddd;
    private static final int CONNECTED_STATE_COLOR = 0x00ff00;
    private static final int LOGIN_ERROR_STATE_COLOR = 0xff0000;
    private static final int NO_CONNECTION_STATE_COLOR = 0xffff00;
    
    public static final int NO_STATE = 0;
    public static final int CONNECTED_STATE = 1;
    public static final int LOGIN_ERROR_STATE = 2;
    public static final int NO_CONNECTION_STATE = 3;
    
    protected static boolean refreshTheme = false;
    
    private static Timer updateTimer;
    private static Runnable timerListener;
    protected static int state = NO_STATE;
    
    private Label titleLabel;
    private Label timeLabel;
    private Label stateLabel;
    
    public BasicForm()
    {
        super();
        
        init(); 
    }
    
    public BasicForm(String title)
    {
        super(title);
        
        init();
    }
    
    private void init()
    {   
        titleLabel = new Label(super.getTitle());
        titleLabel.setEndsWith3Points(true);
        titleLabel.setUIID("Title");
        titleLabel.setPreferredW(getWidth() - TITLE_ELEMENT_PADDING * 2 - STATE_LABEL_WIDTH - 40);
        titleLabel.getStyle().setPadding(Component.LEFT, 15);
        //getTitleArea().replace(getTitleComponent(), titleLabel, null);
        getTitleArea().addComponent(BorderLayout.WEST, titleLabel);
                    
        timeLabel = new Label(DateUtils.getCurrentShortTime());
        timeLabel.setUIID("Title");
        timeLabel.setEndsWith3Points(false);
        timeLabel.setAlignment(Label.RIGHT);
        timeLabel.getStyle().setPadding(Component.RIGHT, TITLE_ELEMENT_PADDING);
        //getTitleArea().addComponent(BorderLayout.CENTER, timeLabel);
        getTitleArea().replace(getTitleComponent(), timeLabel, null);
        
        stateLabel = new Label("");
        stateLabel.setPreferredW(STATE_LABEL_WIDTH);
        stateLabel.getStyle().setPadding(0, 0, 0, 0);
        stateLabel.getStyle().setMargin(0, 0, 0, 0);
        stateLabel.getStyle().setBgColor(getStateColor());
        getTitleArea().addComponent(BorderLayout.EAST, stateLabel);
        
        //Запускаем таймер обновления
        startUpdateTimer();
    }
    
    public void show()
    {
        //Обновляем заголовок
        run();
        
        //Устанавливаем себя слушателем таймера
        setTimerListener();
        
        //Обновляем тему 
        if (refreshTheme)
        {
            titleLabel.refreshTheme(); 
            timeLabel.refreshTheme(); 
            
            refreshTheme(); 
            
            //Возвращаем назад компоненты
            getTitleArea().addComponent(BorderLayout.WEST, titleLabel);
            getTitleArea().replace(getTitleComponent(), timeLabel, null);
            getTitleArea().addComponent(BorderLayout.EAST, stateLabel); 
        }
        
        super.show();
    }
    
    private void setTimerListener()
    {
        timerListener = this;
    }
    
    public void setTitle(String title)
    {
        titleLabel.setText(title);
        titleLabel.repaint();
    }
    
    public String getTitle()
    {
        return titleLabel.getText();
    }
    
    public void run()
    {
        timeLabel.setText(DateUtils.getCurrentShortTime());        
        stateLabel.getStyle().setBgColor(getStateColor());
        
        repaint();
    }
    
    private static int getStateColor()
    {
        switch(state)
        {
            case NO_STATE : return NO_STATE_COLOR;
            case CONNECTED_STATE : return CONNECTED_STATE_COLOR;
            case LOGIN_ERROR_STATE : return LOGIN_ERROR_STATE_COLOR;
            case NO_CONNECTION_STATE : return NO_CONNECTION_STATE_COLOR;
            default : return NO_STATE_COLOR;
        }
    }
    
    public void setState(int newState)
    {
        state = newState;
        
        //Обновляем и перерисовываем
        stateLabel.getStyle().setBgColor(getStateColor());
        stateLabel.repaint();
    }
    
    private void startUpdateTimer()
    {
        if (updateTimer == null)
        {   
            updateTimer = new Timer();
            updateTimer.schedule(new TimerTask()
            {
                public void run()
                {
                    if (timerListener != null)
                    {
                        timerListener.run();
                    }
                }
            }, UPDATE_TIMER_INTERVAL, UPDATE_TIMER_INTERVAL);
        }
    }
    
    public void hide()
    {
        hideNotify();
    }
}
